<?php
class HomeAction extends CommonAction{
	public $usertype;	//1:主人(自己的主页)    2:访客(已登录的)   3:游客(未登录的)
	public $id;			//主页的ID get('id');
	public $uid;		//Session 的ID
	
	//id的检测及加载用户类型检测
	public function __construct(){
		$this->uid = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$idres = M()->query('SELECT UserId FROM teacher WHERE UserId='.$this->_get('id')); 	//请修改此处查询的ID及表名
		$this->id = $idres[0]['UserId'];
		//还有此处的字段名
		if(!$this->id)
			$this->id = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		if(!$this->id)
			echo '<script>window.history.back(-1);</script>';
		$this->getusertype();
	}
	
	//判断用户类型
	public function getusertype(){
		if($this->id==$this->uid){
			$this->usertype=1;
		}else{
			if($this->uid)
				$this->usertype=2;
			else
				$this->usertype=3;
		}
		$this->assign('usertype',$this->usertype);
	}
	public function index(){
		//查询出主页用户的基本信息
		$Model=new Model();
		$user = $Model->query("select u.IsJury,u.SchoolId,h.SchoolName,u.TrueName,u.UserId,u.OrgName,u.Avatar,u.LastLoginDate,u.UserType,t.DepartmentName,t.Job,t.QQ from users u,teacher t ,highschool h where t.UserId=u.UserId and t.UserId={$this->id} and u.SchoolId=h.Id");
		$this->assign('uid',$user['0']['UserId']);	//主页链接传参用
		$this->assign('user',$user);
		$this->assign('id',$this->id);
		//查询老师指导的团队
		$this->display('index');
	}
	public function editBaseInfo(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
		//$uid=1;
		if(!($this->_post("new_pass"))){
			$user = M('Users')->where("UserId={$uid}")->field('TrueName,PhoneNumber,Email')->find();
			$this->assign('user',$user);
			$this->display('editBaseInfo');
		}else{
			$data=array(
				"TrueName" => "{$this->_post('TrueName')}",
				"PhoneNumber" => "{$this->_post('PhoneNumber')}",
				"Email" => "{$this->_post('Email')}",
			);
			edit_base_info($uid,$data);
			$this->success('修改基本信息成功');
		}
	}

	//详细信息
	public function editDetailinfo(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
		//$uid=1;
		if(!$_POST){
			$user = M('teacher')->where("UserId={$uid}")->find();
			$this->assign('user',$user);
			$this->display('editDetailInfo');
		}else{
			$data=array(
				"EduLevel" => "{$this->_post('EduLevel')}",
				"DepartmentName" => "{$this->_post('DepartmentName')}",
				"Job" => "{$this->_post('Job')}",
				"Title" => "{$this->_post('Title')}",
				"Address" => "{$this->_post('Address')}",
				"PostalCode" => "{$this->_post('PostalCode')}",
				"FixedPhone" => "{$this->_post('FixedPhone')}",
				"OtherPhone" => "{$this->_post('OtherPhone')}",
				"MSN" => "{$this->_post('MSN')}",
				"QQ" => "{$this->_post('QQ')}",
				"Fetion" => "{$this->_post('Fetion')}",
				"PersonalIntroduce" => "{$this->_post('PersonalIntroduce')}",
			);
			edit_detail_info($uid,$data);
			$this->success('修改详细信息成功');
		}
	}
	public function editPassword(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
	 	if(!$this->_post("new_pass")){
			//加载修改界面
			$this->display('editPassword');
		}else{
			//判断原密码是否匹配
		 	$m = M('users')->where("UserId={$uid}")->find();
			$oldpass=md5(trim($this->_post('old_pass')));
			if($m['Password'] != $oldpass){
				$this->error('原始密码错误');//跳转 or 弹窗?
			}
			if($this->_post('new_pass') != $this->_post('check_pass')){
				$this->error('确认密码不一致','editPassword',1);
			}else{
				//修改密码
				edit_pass($uid,$this->_post('new_pass'));
				$this->success('修改密码成功');
			}  			
		} 
	}
	public function editAvatar(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
		//$uid=1;
		$list = M('Users')->where("UserId={$uid}")->find();
		$this->assign('list',$list);
		$this->display('editAvatar');
	}
	public function editAvatarAjax(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$OldAvatar = M('Users')->where("UserId={$uid}")->field('Avatar')->find();
		import('ORG.Net.UploadFile');
		import("ORG.Util.Image");
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 1048576 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','bmp');// 设置附件上传类型
		$upload->savePath = './UpFile/img/Avatar/';// 设置附件上传目录
		$upload->thumb = true; 
		//设置需要生成缩略图的文件后缀 
		$upload->thumbPrefix = 's_';
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = '165';
        //设置缩略图最大高度
        $upload->thumbMaxHeight = '165';
        //删除原图
        $upload->thumbRemoveOrigin = true;
		if(!$upload->upload()) {
			// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{
			// 上传成功 获取上传文件信息
			$info = $upload->getUploadFileInfo();
			$Avatar = 's_'.$info[0]['savename'];
			edit_avatar($uid,$Avatar,$OldAvatar);
		}
	}
	
	//个人主页选项卡
	public function a0(){
		$type=$this->_get('type');
		@$page = $this->_get('page');
		if(!intval($page))
			$page=1;
		$info = new InfoModel($this->_get('id'),$page,$type);
		if(!$info->uid)
			echo '<script>alert("非法操作");window.history.back(-1);</script>';
		$this->assign('uid',$info->uid);				
		$this->assign('list',$info->list);				
		$this->assign('pagenum',$info->pagenum);		//总页数
		$this->assign('page',$info->page);				//当前页
		switch($type){
			case 0:
				$this->display('a0');					//我的点评
			break;
			case 1:
				$this->display('a1');					//我的博文
			break;			
			case 2:
				$this->display('a2');					//学习资源
			break;
		}
	}

    public function instructorsTeamList(){

		$list = M()->query("select i.Id,t.Logo,t.Name,t.CaptainName,t.Declaration from team t,teaminstructors i where t.Id=i.TeamId and i.InstructorId={$this->uid} and i.IsApproved=0 order by i.Id desc");
		//dump($list);
		$this->assign('list',$list);
    	$this->display();
    }
    public function instructorsTeamApproved(){
		
		$teaminstructors = M('Teaminstructors');
		$data['IsApproved'] = $this->_post('aa');
		$teaminstructors->where("Id={$this->_post('id')}")->save($data);

    }
	public function ResumeList(){
		$Users = M('Users'); // 实例化User对象
		import('ORG.Util.Page');// 导入分页类
		$count = M()->query("select count(*) num from users u1 , users u2 where u1.UserType=2 and u1.SchoolId=u2.SchoolId and u2.UserId={$this->id}");// 查询满足要求的总记录数
		$Page = new Page($count[0]['num'],10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show();// 分页显示输出

		$list = M()->query("select u1.UserId,u1.Avatar,u1.TrueName from users u1 , users u2 where u1.UserType=2 and u1.SchoolId=u2.SchoolId and u2.UserId={$this->id} limit {$Page->firstRow},{$Page->listRows}");
		
		$this->assign('list',$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
}